package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyYueShangXingV2;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.LinkedHashMap;
import java.util.Map;
/**
 *  悦尚祥
 *
 *  @author 李源聪
 */
@ApiPlan(productId = "58", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5YueShangXiangV2 implements ApiOrgPhoneMd5ConvertHandler {

    private static final String KEY = "eGF91TEosRe6Vum2";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        Map<Object, Object> newMap = new LinkedHashMap<>();
        newMap.put("tel", apiDataExchangeParameter.getPhoneMd5());
        return newMap;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {

        OrgApplyYueShangXingV2 data = new OrgApplyYueShangXingV2();

        data.setCname(apiApplyParameter.getUserName());

        data.setTel(apiApplyParameter.getPhone());

        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);

        data.setAge(apiFieldConverter.getAgeInt());

        //  性别 1男/2女
        String sex = apiApplyParameter.getSex();
        Integer sexChoose = ChooseEq.create()
                .when(sex, "0", 1)
                .when(sex, "1", 2)
                .end(1);
        data.setSex(sexChoose);

        // 职业
        if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
            String career = ChooseEq.create()
                    .when(apiApplyParameter.getCareer(), "1", "上班族")
                    .when(apiApplyParameter.getCareer(), "2", "自由职业")
                    .when(apiApplyParameter.getCareer(), "3", "企业主")
                    .when(apiApplyParameter.getCareer(), "4", "公务员或事业单位")
                    .when(apiApplyParameter.getCareer(), "5", "个体户")
                    .end();
            data.setJob(career);
        }

        //  贷款额度 （单位：万）
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            String money = ChooseEq.create()
                    .when(apiApplyParameter.getLoanAmount(), "1", "3")
                    .when(apiApplyParameter.getLoanAmount(), "2", "5")
                    .when(apiApplyParameter.getLoanAmount(), "3", "10")
                    .when(apiApplyParameter.getLoanAmount(), "4", "20")
                    .end();
            data.setEd(money);
        }

        //  房产情况 0无/1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            Integer house = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                    .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                    .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                    .when(apiApplyParameter.getRealEstateInfo(), "4", 1)
                    .when(apiApplyParameter.getRealEstateInfo(), "5", 1)
                    .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                    .end(0);
            data.setFc(house);
        }

        //  车 0无/1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            Integer car = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", 0)
                    .when(apiApplyParameter.getCarInfo(), "2", 1)
                    .when(apiApplyParameter.getCarInfo(), "3", 1)
                    .when(apiApplyParameter.getCarInfo(), "4", 1)
                    .end(0);
            data.setCc(car);
        }

        //  保单 0无/1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            Integer gongjijin = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", 0)
                    .when(apiApplyParameter.getInsurancePolicy(), "2", 1)
                    .when(apiApplyParameter.getInsurancePolicy(), "3", 1)
                    .when(apiApplyParameter.getInsurancePolicy(), "4", 1)
                    .end(0);
            data.setBd(gongjijin);
        }

        //  公积金 0无/1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            Integer gongjijin = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", 0)
                    .when(apiApplyParameter.getProvidentFund(), "2", 1)
                    .when(apiApplyParameter.getProvidentFund(), "3", 1)
                    .when(apiApplyParameter.getProvidentFund(), "4", 1)
                    .end(0);
            data.setGjj(gongjijin);
        }

        //  社保 0无/1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            Integer shebao = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", 0)
                    .when(apiApplyParameter.getSocialSecurity(), "2", 1)
                    .when(apiApplyParameter.getSocialSecurity(), "3", 1)
                    .when(apiApplyParameter.getSocialSecurity(), "4", 1)
                    .end(0);
            data.setSb(shebao);
        }

        // 收入
        if (ValidParam.isNotEmpty(apiApplyParameter.getMonthlySalary())) {
            Number monthlySalaryNumber = apiFieldConverter.getMonthlySalaryNumber();
            if (monthlySalaryNumber != null) {
                data.setSr(monthlySalaryNumber.toString());
            } else {
                data.setSr("未知");
            }
        } else {
            data.setSr("未知");
        }


        // 芝麻分
        data.setZmf(apiFieldConverter.getSesameCreditText());

        // 城市
        String city = apiApplyParameter.getCity();
        if (ValidParam.isNotNull(city)) {
            data.setCity(city.replace("市", ""));
        }
        data.setFromId(110);
        Map<Object, Object> newMap = new LinkedHashMap<>();
        AES aes = AES.createEcb(KEY.getBytes());
        newMap.put("data",aes.encryptToBase64Str(JsonUtils.toJson(data)));
        return newMap;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
